/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System;
using System.ComponentModel;

namespace Xceed.Silverlight.Controls
{
  [TemplateVisualState( GroupName = "LicensingStates", Name = "Licensed" )]
  [TemplateVisualState( GroupName = "LicensingStates", Name = "NotLicensed" )]
  public class AutoSelectTextBox : TextBox, ISupportInitialize
  {
    #region AutoSelectBehavior PROPERTY

    public AutoSelectBehavior AutoSelectBehavior
    {
      get
      {
        return ( AutoSelectBehavior )GetValue( AutoSelectBehaviorProperty );
      }
      set
      {
        SetValue( AutoSelectBehaviorProperty, value );
      }
    }

    public static readonly DependencyProperty AutoSelectBehaviorProperty =
        DependencyProperty.Register( "AutoSelectBehavior", typeof( AutoSelectBehavior ), typeof( AutoSelectTextBox ),
      new PropertyMetadata( AutoSelectBehavior.Never ) );

    #endregion AutoSelectBehavior PROPERTY

    #region Contructor

    public AutoSelectTextBox()
    {


      this.TextChanged += new TextChangedEventHandler( this.OnTextChanged );

      this.Loaded += new RoutedEventHandler( this.OnLoaded );
      this.Unloaded += new RoutedEventHandler( this.OnUnloaded );
    }

    private void OnLoaded( object sender, RoutedEventArgs e )
    {
      this.ChangeVisualState( false );
    }

    private void OnUnloaded( object sender, RoutedEventArgs e )
    {
      this.ChangeVisualState( false );
    }

    #endregion

    protected override void OnKeyDown( KeyEventArgs e )
    {
      base.OnKeyDown( e );

      if( !e.Handled )
      {
        if( ( Keyboard.Modifiers & ModifierKeys.Shift ) == ModifierKeys.Shift )
        {
          switch( e.Key )
          {
            case Key.Up:
            case Key.Left:
              //Shift-Left with selection at beginning.
              //Shift-Up with selection at beginning.
              if( this.SelectionStart == 0 )
              {
                e.Handled = true;
              }
              break;

            case Key.Right:
            case Key.Down:
              //Shift-Right with selection at end.
              //Shift-Down with selection at end.
              if( ( this.SelectionStart + this.SelectionLength ) == this.Text.Length )
              {
                e.Handled = true;
              }
              break;

            default: // do nothing
              break;
          }
        }
      }

    }

    protected override void OnGotFocus( RoutedEventArgs e )
    {
      base.OnGotFocus( e );

      if( this.AutoSelectBehavior == AutoSelectBehavior.OnFocus )
      {
        this.SelectAll();
      }
    }

    protected virtual void OnTextChanged( object sender, TextChangedEventArgs e )
    {
      BindingExpression bindingExpression = this.GetBindingExpression( TextBox.TextProperty );

      if( bindingExpression != null )
        bindingExpression.UpdateSource();
    }

    protected void ChangeVisualState()
    {
      this.ChangeVisualState( true );
    }

    protected void ChangeVisualState( bool useTransitions )
    {
      this.UpdateLicensingStates( useTransitions );
      this.ChangeVisualStateCore( useTransitions );
    }

    protected internal virtual void ChangeVisualStateCore( bool useTransitions )
    {
    }

    private void UpdateLicensingStates( bool useTransitions )
    {
      if( m_isLicensed )
      {
        VisualStateManager.GoToState( this, "Licensed", useTransitions );
      }
      else
      {
        VisualStateManager.GoToState( this, "NotLicensed", useTransitions );
      }
    }

    #region Private fields

    //licensing
    private readonly Guid m_designTimeGuid;
    private readonly bool m_isInDesigner;
    private bool m_isLicensed = true;

    #endregion

    #region ISupportInitialize Members

    void ISupportInitialize.BeginInit()
    {
      m_iSupportInitializeCount++;

      if( m_iSupportInitializeCount == 1 )
      {
        this.BeginInitCore();
      }
    }

    void ISupportInitialize.EndInit()
    {
      if( m_iSupportInitializeCount == 0 )
        return;

      m_iSupportInitializeCount--;

      if( m_iSupportInitializeCount == 0 )
      {
        this.IsInitialized = true;
        this.EndInitCore();
      }
    }

    protected bool IsInitialized
    {
      get;
      private set;
    }

    protected virtual void BeginInitCore()
    {
    }

    protected virtual void EndInitCore()
    {
    }

    private int m_iSupportInitializeCount;

    #endregion
  }
}
